﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace BDRUtils.Utility
{
    public static class Cryptography
    {
        /// <summary>
        /// MD5加密字符串（32位大写）
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string GetMD5(this string source)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] bytes = Encoding.UTF8.GetBytes(source);
            string result = BitConverter.ToString(md5.ComputeHash(bytes));
            return result.Replace("-", "");
        }

        /// <summary>
        /// Sha1加密
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Sha1(this string value)
        {
            var buffer = Encoding.UTF8.GetBytes(value);
            var data = SHA1.Create().ComputeHash(buffer);
            var sb = new StringBuilder();
            foreach (var t in data)
            {
                sb.Append(t.ToString("X2"));
            }
            return sb.ToString();
        }
    }
}
